#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

PID=`pidof -o %PPID /usr/bin/minbif`
case "$1" in
  start)
    stat_busy "Starting minbif daemon"
    if [ -z "$PID" ]; then
      if [ ! -d /var/run/minbif ]; then
        mkdir -p /var/run/minbif && chown minbif:minbif /var/run/minbif
      fi
      [ -f /var/run/minbif/minbif.pid ] && rm -f /var/run/minbif/minbif.pid
      CMD='/usr/bin/minbif --pidfile /var/run/minbif/minbif.pid \
                           /etc/minbif/minbif.conf'
      su -s /bin/sh -c "$CMD" minbif

      if [ $? -gt 0 ]; then
        stat_fail
      else
        add_daemon minbif
        stat_done
      fi
    else
      stat_fail
    fi
    ;;

  stop)
    stat_busy "Stopping minbif daemon"
    [ ! -z "$PID" ] && kill $PID &> /dev/null
    if [ $? -gt 0 ]; then
      stat_fail
    else
      rm -f /var/run/minbif/minbif.pid
      rm_daemon minbif
      stat_done
    fi
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  *)
    echo "usage: $0 {start|stop|restart}"
esac
exit 0
